<?php
Database($_SESSION['cid']);
header("content-type:text/html;charset=utf-8");
class MeetingAction extends Action{
//--------------------------------------insert-------------------------------------------------------------
	public function add_meeting()
	{
		$rs = D('Meeting')->add_meeting($_POST);
		if($rs)
			$this->success('添加成功');
		else
			$this->error('添加失败');
	}
//--------------------------------------update-------------------------------------------------------------
//--------------------------------------delete-------------------------------------------------------------
	public function delete_meeting()
	{
		$data['time_delete'] = time();
		$rs = M('meeting','','DB_MEETING')->where(array('meeting_id' =>$_GET['meeting_id']))->delete();
		if($rs)
			echo json_encode(array('state'=>0));
		else
			echo json_encode(array('state'=>-1));
	}
	public function del_category_act(){
		$data['time_delete'] = time();
		$rs = M('meeting_category','','DB_MEETING')->where(array('category_id' => $_GET['category_id']))->save($data);
		if($rs)
			$this->success('删除成功');
		else
			$this->error('删除失败');
	}
//--------------------------------------select-------------------------------------------------------------
    public function meeting_list()
    {
		$righs = D('Admin')->get_rights($_SESSION['id']);
		if($righs == 2){
			$re = M('admin_rights','','')->db('CONFIG1')->where('admin_id='.$_SESSION['id'].' and rights_for="meeting"')->getField('rights_id');
			if(!$re)
				$this->error('您没有管理会议的权限');
			$where = $re;
		}else{
			$where = 1;
		}
		$rs = D('Meeting')->pageing($righs,$where);
		foreach($rs['data'] as &$val){
            $val['meeting_startTime']=date('Y-m-d H:i',$val['meeting_startTime']);
            $val['meeting_endTime']  =date('Y-m-d H:i',$val['meeting_endTime']);
			if($val['meeting_category']==0)
				$val['meeting_category']='普通会议';
			else
				$val['meeting_category']= M('meeting_category','','DB_MEETING')->where('category_id='.$val['meeting_category'])->getField('category_name');
        }
		
		if($righs == 2){
			$where = 'time_insert>time_delete and category_id in ('. $re .')';
		}else{
			$where = 'time_insert>time_delete';
		}
		$meeting_category = M('meeting_category','','DB_MEETING')->where($where)->select();
		if($righs == 1)
			$meeting_category[]= array('category_id'=>0,'category_name'=>'普通会议');

		$rs['audience_list']= D('Audience')->get_audience_group_by_department();

		$this->assign('meeting_category',$meeting_category);
        $this->assign("rs",$rs);
        $this->display();
    }

//---------------------------------------------------------------------------------------
/*
 * 会议类型
 * */
    public function meeting_category_list()
    {
        $rs=pageing('meeting_category',100,'',$_GET['seach'],'category_id desc','category_pid=0 and time_insert>time_delete','category_name','','DB_MEETING');
        foreach($rs['data'] as $val)
        {
            $val['pid'] = D('Meeting')->get_son_category($val['category_id']);
            $arr[]      = $val;
        }
        $rs['data']=$arr;
        $this->assign("rs",$rs);
        $this->display();
    }

	public function add_category(){
		$rs['category'] = M('meeting_category','','DB_MEETING')->where('category_pid=0 and time_insert>time_delete')->select();
		$this->assign('rs',$rs);
		$this->display();	
	}
	public function add_category_act(){
		$_POST['time_insert']=time();
		$_POST['time_update']=time();
		$_POST['time_delete']=-1;
		if($_POST['category_name']!='')
			$rs = M('meeting_category','','DB_MEETING')->add($_POST);
		if($rs)
			$this->success('添加成功',__URL__.'/meeting_category_list');
		else
			$this->error('添加失败');
	}

	public function to_update_category_act(){
		$category = M('meeting_category','','DB_MEETING') ->where('category_id='.$_GET['category_id']) ->find();
		$catetype = M('meeting_category','','DB_MEETING') ->where('time_insert>time_delete and category_pid=0')->select();
		$this ->assign("cate" , $category);
		$this ->assign("type" , $catetype);
		$this ->display();
	}
	public function update_category_act(){
		$_POST['time_update'] = time();
		$rs = M('meeting_category','','DB_MEETING')->where('time_insert>time_delete')->select();
		foreach ($rs as $key => $value) {
			$d[] = $value['category_pid'];
		}
		$a = $_POST['category_id'];
		if(in_array($a , $d))
		{
			$this ->error('当前类型下还有分类，将其移动后才能修改');
		}else
		{
			$rs = M('meeting_category','','DB_MEETING') ->where('category_id='.$_POST['category_id']) ->save($_POST);
			if($rs)
				$this->success('修改成功', __URL__.'/meeting_category_list');
			else
				$this->error('修改失败');
		}
	}
	//类别查询
    // public function  sel(){
    //     $pid = $_GET['id']; 
    //     $data = M('meeting_category');
    //     $list = $data -> where('category_pid='.$pid) -> select();
    //     for($i = 0;$i<count($list); $i++){
    //         $string .= "<option value='".$list[$i]['category_id']."'>".$list[$i]['category_name']."</option>";
    //     }
    //     echo $string;
    // }
//--------------------------------------------------------------------------------------------
	
/**
  *create meeting
  *update author: zhanghao
  *update time  : 2014-11-18 11:12
  ********************************************/	
	public function create_meeting(){
		//init param
		$_POST['uid']				= $_SESSION['audience_id'];
		$_POST['meeting_startTime'] = strtotime($_POST['meeting_date'].' '.$_POST['meeting_start_time']);
		$_POST['meeting_endTime'] 	= strtotime($_POST['meeting_date'].' '.$_POST['meeting_end_time']);
		$_POST['meeting_local'] 	= $_POST['meeting_position'];
		$_POST['schedule_date']		= date('Y-m-d',$_POST['meeting_startTime']);
		$_POST['meeting_date']		= $_POST['schedule_date'];
		$_POST['time_insert']		= time();
		//schedule meeting room 
		if(!(D('Meeting')->schedule_meeting_room($_POST)))
			$this->error('当前会议室部分时间已经被占用');
		//create meeting
		$rs=D('Meeting')->create_meeting($_POST);
		if($rs){
			$this->success('会议创建成功');
		}else{
			$this->error('当前会议室部分时间已经被占用');
		}
	}
/**
  *select meeting rooom list
  *update author: zhanghao
  *update time  : 2014-11-25 15:50
  ********************************************/
	public function check_idle_meeting_room(){
		$_SESSION['audience_id'] = 1;

		$righs = D('Admin')->get_rights($_SESSION['id']);
		if($righs == 2){
			$re = M('admin_rights','','')->db('CONFIG1')->where('admin_id='.$_SESSION['id'].' and rights_for="meeting"')->getField('rights_id');
			if(!$re)
				$this->error('您没有管理会议的权限');
			$where = 'category_id in ('.$re.')';
		}else{
			$where = 1;
		}
		
		$data['uid']		= $_SESSION['audience_id'];
		$rs['meeting_list']	= json_encode(D('Meeting')->get_meeting_list($data));
		$rs['local']		= D('Meeting')->get_local();
		$rs['audience_list']= D('Audience')->get_audience_group_by_department();

		$meeting_category = M('meeting_category','','DB_MEETING')->where($where)->select();
		$this->assign('meeting_category',$meeting_category);
		$this->assign('rs',$rs);
		$this->display();
	}
	
	public function get_meeting_building(){
		$rs['building_list']=D('Meeting')->get_building_list_by_local_id($_POST['local_id']);
		echo json_encode($rs);
	}
	
	public function get_meeting_floor(){
		$rs['floor_list']=D('Meeting')->get_floor_list_by_building_id($_POST['building_id']);
		echo json_encode($rs);
	}
	
	public function get_meeting_room(){
		$_POST['meeting_date']=date('Y-m-d',strtotime($_POST['meeting_date']));
		$rs=D('Meeting')->check_idle_meeting_room($_POST);
		foreach($rs as $val){
			$val['have_schedule']=$val['have_schedule']==true?1:0;
			$arr[]=$val;
		}
		echo json_encode($arr);	
	}
	
/**
 *select meeting rooom list
 *update author: zhanghao
 *update time  : 2014-11-25 15:50
 */	
 	public function get_meeting_list(){
		$this->display();
	}
	
	
//会议是列表
	public function index(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$arr=array("local_id","room_allSeats","room_haveProjector","room_haveWifi");		
		$city = D('Company/Meeting')->get_local();
		
		$data = page_room("meeting_room",3,'',$_GET['seach'],'room_id desc','1',$arr,'',"DB_MEETING","room_allSeats");
		$sql = 'select a.*,b.floor_name,c.building_name,d.local_name from meeting_room a';
		$sql.= ' join meeting_roomfloor b on a.floor_id = b.floor_id';
		$sql.= ' join meeting_roombuilding c on b.building_id = c.building_id';
		$sql.= ' join meeting_roomlocal d on c.local_id=d.local_id';
		
		$datas = M("meeting_room",'','DB_MEETING')->query($sql);
		
		foreach($datas as $k=>$v){
			$room[$v['room_id']] = $v;
		}

		$this->assign("data",$data['data']);
		$this->assign("show",$data['show']);
		$this->assign("city",$city);
		$this->assign("room",$room);
		$this->display();
	}
//会议室查找	
	public function search(){
		if($_POST){
			if($_POST['page']){
				$_GET['page'] = $_POST['page'];
				unset($_POST['page']);
			}
			if($_POST['room_allSeats'] !=""){
				$seats = explode("_",$_POST['room_allSeats']);
				if($seats){
					for($i=0;$i<count($seats);$i++){
						if($i%2==0){
							$seat[$seats[$i]] = $seats[$i+1];
						}						
					}					
				}
			}else{
				unset($_POST['room_allSeats']);
			}
			if($_POST['name'] !="" && $_POST['name'] !="room_no"){
				$_POST[$_POST['name']]= 1;
				unset($_POST['name']);
			}else{
				$_POST['room_haveProjector']= 0;
				unset($_POST['name']);
			}
			foreach($_POST as $k=>$post){
					$arr[] = $k;
					$_GET['seach'][$k]= $post;
			}
			$_GET['seach']['room_allSeats'] = $seat;
			
		}

		$arr=array("local_id","room_allSeats","room_haveProjector","room_haveWifi");		
		$city = D('Company/Audience')->get_data('meeting_roomlocal');
		$data = page_array_ajax("meeting_room",1,'',$_GET['seach'],'room_id desc','1',$arr,'',"DB_MEETING","room_allSeats");				
		$datas = D("meeting_room")->join('meeting_roomlocal on meeting_roomlocal.local_id=meeting_room.local_id')->join('meeting_roombuilding on meeting_room.building_id = meeting_roombuilding.building_id')->join('meeting_roomfloor on meeting_room.floor_id = meeting_roomfloor.floor_id')->field('meeting_room.*,meeting_roomfloor.floor_name,meeting_roombuilding.building_name,meeting_roomlocal.local_name')->select();		
		foreach($datas as $k=>$v){
			$room[$v['room_id']] = $v;
		}
		
		$this->assign("data",$data['data']);
		$this->assign("show",$data['show']);
		$this->assign("city",$city);
		$this->assign("room",$room);
		$this->display();
	}

//会议室添加	
	public function forAct(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$city = D('Company/Meeting')->get_local();
		$build = D('Company/Meeting')->get_build();
		$floor = D('Company/Meeting')->get_floor();
		$this->assign('floor',$floor);
		$this->assign('build',$build);
		$this->assign('city',$city);
		$this->assign("name",$name);
		
		$this->display();
	}

//城市添加
	public function add_city(){		
		if($_POST['local_name']){
			$post['local_name']=$_POST['local_name'];
			$city_add = D('Company/Meeting')->add_local($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "添加失败";
			}
		}
	}
	
//楼座添加
	public function add_build(){		
		if($_POST['building_name']){
			$post['building_name']=$_POST['building_name'];
			$city_add = D('Company/Meeting')->add_build($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "添加失败";
			}
		}
	}

//楼层添加
	public function add_floor(){		
		if($_POST['floor_name']){
			$post['floor_name']=$_POST['floor_name'];
			$city_add = D('Company/Meeting')->add_floor($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "添加失败";
			}
		}
	}
	
	public function addAct(){
		if($_POST){			
			$room=$_POST;
			$add_room = D('Company/Meeting')->add_room($room);
			if($add_room){
				$this->success("会议室添加成功",__APP__."/Meeting/index");
			}else{
				$this->error("会议室添加失败",__APP__."/Meeting/forAct");
			}
		}
	
	}
	
	public function delAct(){		
		if($_POST){
			$data = D('Company/Meeting')->del_room($_POST['room_id']);
			if($data){
				echo "删除成功";
			}else{
				echo "删除失败";
			}
		}
	}

//会议室修改	
	public function to_update(){
		if($_POST){
			//$sql = "select meeting_room.*,meeting_roomlocal.local_name,meeting_roombuilding.building_name,meeting_roomfloor.floor_name from meeting_room,meeting_roombuilding,meeting_roomlocal,meeting_roomfloor where 'meeting_room.room_id,meeting_roomlocal.local_id,meeting_roombuilding.building_id,meeting_roomfloor.floor_id in(select room_id,local_id,building_id,floor_id from meeting_room where room_id=".$_POST['room_id'].")'";
			//$room = M('Meeting_room','','DB_MEETING')->query($sql);
			
			$room  = D('Company/Meeting')->find_room($_POST['room_id']);			
			$city  = D('Company/Meeting')->get_local();			
			$build = D('Company/Meeting')->get_build();
			$floor = D('Company/Meeting')->get_floor();			
		}
		$this->assign('room',$room);				
		$this->assign('floor',$floor);
		$this->assign('build',$build);
		$this->assign('city',$city);
		$this->display();		
	}

//会议室修改
	public function saveAct(){		
		if($_POST){			
			$room=$_POST;
			$save = D('Company/Meeting')->save_room($room);
			if($save){
				$this->success("会议室修改成功",__APP__."/Meeting/index");
			}else{
				$this->error("会议室修改失败",__APP__."/Meeting/index");
			}
		}
	}

//--------------------------------------action-------------------------------------------------------------
	public function push_meeting_notice(){
		$meeting_id	  = $_GET['meeting_id'];
		$action 	  = $_GET['action'];
		$meeting_name = $_GET['name'];

		if($action == 'delete')
			$title = '会议取消通知';

		$post_data['push_key_words'] = 'meeting';
		$post_data['message']        = "【".$title."】".$meeting_name;
		$post_data['message']        = mb_substr($post_data['message'],0,30);
		$post_data['message']        = nl2br($post_data['message']);
		$post_data['message']        = strip_tags( $post_data['message']);

		$tokens = D('Uri')->get_meeting_uri($meeting_id);
		foreach($tokens as $val){
			if($val['mobiletype'] == 1){
				$post_data['tokens'][] = array(
					'token' => $val['devicetoken'],
					'badge' => (int)$val['badge'] + 1
				);
				$arr['app_for'] = 'meeting';
				$arr['uid']     = $val['uid'];
				D('Push')->update_badge_number(((int)$val['badge'] + 1),$arr);
			}
		}
		A('Company/Push')->push_more($post_data);

	}

}
?>